package com.sky.controller.user;

import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersPaymentDTO;
import com.sky.dto.OrdersSubmitDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.Orderservice;
import com.sky.vo.OrderPaymentVO;
import com.sky.vo.OrderSubmitVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController("userOrderController")
@RequestMapping("/user/order")
@Api(tags = "用户订单")
@Slf4j
public class OrderController {
	
	@Autowired
	private Orderservice orderservice;
	
	/**
	 * 查询订单
	 */
	@GetMapping("/orderDetail/{id}")
	@ApiOperation("查询订单")
	public Result<OrderVO> getOrder(@PathVariable Long id) {
		log.info("查询订单：{}", id);
		return Result.success(orderservice.getOrder(id));
	}
	
	/**
	 * 用户下单
	 */
	@PostMapping("/submit")
	@ApiOperation("用户下单")
	public Result<OrderSubmitVO> submitOrder(@RequestBody OrdersSubmitDTO ordersSubmitDTO) {
		log.info("用户下单：{}", ordersSubmitDTO);
		return Result.success(orderservice.submitOrder(ordersSubmitDTO));
	}
	
	/**
	 * 订单支付
	 *
	 * @param ordersPaymentDTO
	 * @return
	 */
	@PutMapping("/payment")
	@ApiOperation("订单支付")
	public Result<OrderPaymentVO> payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO) throws Exception {
		log.info("订单支付：{}", ordersPaymentDTO);
		OrderPaymentVO orderPaymentVO = orderservice.payment(ordersPaymentDTO);
		log.info("生成预支付交易单：{}", orderPaymentVO);
		// 模拟交易成功，修改订单状态
		orderservice.paySuccess(ordersPaymentDTO.getOrderNumber());
		return Result.success(orderPaymentVO);
	}
	
	/**
	 * 查询历史订单
	 */
	@GetMapping("/historyOrders")
	@ApiOperation("查询历史订单")
	public Result<PageResult> getOrderList(OrdersPageQueryDTO ordersPageQueryDTO) {
		log.info("查询历史订单：{}", ordersPageQueryDTO);
		return Result.success(orderservice.getOrderList(ordersPageQueryDTO));
	}
	
	/**
	 * 再来一单
	 */
	@PostMapping("/repetition/{id}")
	@ApiOperation("再来一单")
	public Result againOrder(@PathVariable Long id) {
		log.info("再来一单：{}", id);
		orderservice.againOrder(id);
		return Result.success();
	}
	
	/**
	 * 催单
	 */
	@GetMapping("/reminder/{id}")
	@ApiOperation("催单")
	public Result getRemindOrder(@PathVariable Long id) {
		log.info("催单：{}", id);
		orderservice.getRemindOrder(id);
		return Result.success();
	}
	
	/**
	 * 取消订单
	 */
	@PutMapping("/cancel/{id}")
	@ApiOperation("取消订单")
	public Result cancelOrder(@PathVariable Long id) throws Exception {
		log.info("取消订单：{}", id);
		orderservice.cancelOrder(id);
		return Result.success();
	}
}
